<script setup>
// 导入 Footer 组件
import FooterView from '../components/FooterView.vue'
import { ref } from 'vue'
import { Recommended } from '@/api/index'
import { useRouter } from 'vue-router'
const router = useRouter()
const jupmRouter = () => {
  router.push({
    path: '/classify',
  })
}

// 定义 static 目录下的照片数组
const staticImages = [
  { img: new URL('../static/image/1.jpg', import.meta.url).href, id: '1' },
  { img: new URL('../static/image/2.jpg', import.meta.url).href, id: '3' },
  { img: new URL('../static/image/3.jpg', import.meta.url).href, id: '2' },
  { img: new URL('../static/image/4.jpg', import.meta.url).href, id: '7' },
  { img: new URL('../static/image/5.jpg', import.meta.url).href, id: '6' },
  { img: new URL('../static/image/6.jpg', import.meta.url).href, id: '8' },
  { img: new URL('../static/image/7.jpg', import.meta.url).href, id: '12' },
  { img: new URL('../static/image/8.jpg', import.meta.url).href, id: '4' }
]

// 获取轮播图数据
const banner = ref([])
const getBanner = async () => {
  await Recommended().then(content => {
    // console.log(content)
    banner.value = content.data.result.banner

    // console.log(banner.value)
  }).catch(err => {
    console.log(err)
  })
}
// 获取列表数据
const listFn = ref([])
const rList = ref([])
const getList = async () => {
  await Recommended().then(content => {
    console.log(content)
    listFn.value = content.data.result.list
    console.log(listFn.value)
    // 提取 r 数组数据
    rList.value = content.data.result.list.flatMap(item => item.r)
    console.log('提取的 r 数组', rList.value)
  })
    .catch(err => {
      console.log(err)
    })
}
// 初始化
getBanner();
getList();
</script>

<template>
  <div class="home">
    <!-- 头部 -->
    <div class="home-heaer">
      <i class="i-arrow"></i>
      <h3>彪子有料</h3>
    </div>
    <!-- 输入框 -->
    <!-- <div class="home-input">
      <div class="input-container">
        <input type="text" placeholder="炒锅牛肉">
        <i class="home-search"></i>
        <i class="home-del"></i>
      </div>
    </div> -->
    <div class="home-list">
      <ul>
        <li v-for="(item, index) in staticImages" :key="index">
          <router-link :to="{ path: '/food', query: { id: item.id } }">
            <img :src="item.img" alt="">
          </router-link>
        </li>
      </ul>
    </div>
    <!-- 轮播图 -->
    <div class="banner">
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white" :loop="true">
        <van-swipe-item v-for="item in banner" :key="item.id">
          <img :src="item.i" alt="">
        </van-swipe-item>

      </van-swipe>
    </div>
    <!-- 热门搜索 -->
    <div class="home-list-search">
      <button class="home-btn" @click="jupmRouter">全部菜谱分类</button>
      <!-- <h3>热门搜索</h3>
      <div class="home-search-list">
        <ul>
          <li v-for="item in 6">
            <router-link to="/">食材大全</router-link>
          </li>
        </ul>
      </div> -->
    </div>

    <!-- 热门推荐 -->
    <div class="home-popular">
      <h2>热门推荐</h2>
      <ul>

        <li v-for="(item, index) in rList" :key="index">
          <router-link to="/cate">
            <div class="home-popular-box">
              <div class="home-list-left">
                <img :src="item?.img ?? ''" alt="">
              </div>
              <div class="home-right">
                <p>{{ item?.n ?? ''
                }}</p>
                <span class="home-span1">{{ item?.a?.n ?? '' }}</span>
                <div class="home-icon">
                  <div class="home-icon-left">
                    <i class=" icon-1 home-icon-yanj"></i>
                    <span>{{ item?.vc ?? '' }}</span>
                  </div>
                  <div class="home-icon-right">
                    <i class=" icon-1 home-icon-xingx"></i>
                    <span>{{ item?.collect_count_text ?? ''
                    }}</span>
                  </div>
                </div>
              </div>
            </div>
          </router-link>
        </li>

      </ul>
    </div>
  </div>
  <!-- 使用 Footer 组件 -->
  <FooterView />
</template>
<style lang="less">
@import url('../assets/main.css');

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;

}

.home {
  width: 100vw;
  margin: 0 auto;
  overflow: hidden;
  padding-bottom: 60px;
  // padding-top: 60px;

  .home-heaer {
    width: 100%;
    position: fixed;
    height: 60px;
    line-height: 60px;
    background-color: #FFA940;
    align-items: center;
    display: flex;
    padding: 0 15px;
    z-index: 1000;

    .i-arrow {
      display: inline-block;
      width: 24px;
      height: 32px;
      background-image: url("../assets/image/arrow.png");
      background-size: cover;
      background-position: center;
      background-repeat: no-repeat;
      margin-right: 10px;
    }

    h3 {
      font-size: 30px;
      font-family: aaa;
    }
  }

  // .home-input {
  //   margin-top: 60px;
  //   padding: 20px 20px;

  //   .input-container {
  //     position: relative;

  //     input {
  //       width: 100%;
  //       height: 40px;
  //       font-family: aaa;
  //       font-size: 16px;
  //       border-radius: 5px;
  //       border: 1px solid #ccc;
  //       padding-left: 30px;
  //       padding-right: 30px;
  //       font-size: 18px;
  //       font-weight: bold;
  //     }

  //     .home-search {
  //       position: absolute;
  //       left: 10px;
  //       /* 距离左侧 10px */
  //       top: 50%;
  //       transform: translateY(-50%);
  //       display: inline-block;
  //       width: 20px;
  //       height: 20px;
  //       background-image: url("../assets/image/search.png");
  //       /* 替换为实际搜索图标路径 */
  //       background-size: cover;
  //       background-position: center;
  //       background-repeat: no-repeat;
  //       fill: #999999;
  //     }

  //     .home-del {
  //       position: absolute;
  //       right: 10px;
  //       /* 距离右侧 10px */
  //       top: 50%;
  //       transform: translateY(-50%);
  //       display: inline-block;
  //       width: 16px;
  //       height: 16px;
  //       background-image: url("../assets/image/dle.png");
  //       /* 替换为实际删除图标路径 */
  //       background-size: cover;
  //       background-position: center;
  //       background-repeat: no-repeat;
  //       fill: #999999;
  //     }
  //   }
  // }

  .home-list {
    margin-top: 60px;
    padding: 0 20px;

    ul {
      display: flex;
      flex-wrap: wrap;
      list-style: none;
    }

    li {
      flex: 0 0 25%;
      /* 每个 li 元素占父容器宽度的 25%，一行刚好 4 个 */
      padding: 5px;
      /* 可选，添加内边距使照片之间有间距 */
      box-sizing: border-box;
    }

    img {
      width: 100%;
      height: auto;
      display: block;
    }
  }

  .banner {
    padding: 20px 20px;
    width: 100%;

    .my-swipe {
      // height: 200px;
      width: 100%;

      .van-swipe-item {
        color: #fff;
        font-size: 20px;
        // line-height: 200px;
        text-align: center;
        // background-color: #39a9ed;
        border-radius: 10px;
        width: 100%;

        img {
          width: 100%;
          object-fit: cover;
          border-radius: 10px;

        }
      }
    }
  }

  // 热门搜索
  .home-list-search {
    padding: 10px 20px;

    button {
      width: 100%;
      height: 40px;
      border-radius: 5px;
      border: 0;
      background-color: #F4B644;
      font-family: aaa;
      color: #fff;
    }

    h3 {
      margin-top: 15px;
      font-family: aaa;
      font-size: 24px;
      color: #302e2e;
      font-weight: bold;
    }



  }

  .home-popular {
    padding: 0 20px;

    h2 {
      font-family: aaa;
      font-size: 20px;
      color: #302e2e;
      font-weight: bold;
      margin-bottom: 10px;
    }

    ul {
      list-style: none;

      li {
        display: flex;
        // align-items: center;
        // 盒子居中
        justify-content: center;
        margin-bottom: 10px;
        border-bottom: 2px solid #ccc;

        .home-popular-box {
          display: flex;
          align-items: center;

          justify-content: center;
          margin-bottom: 20px;

          .home-list-left {
            width: 150px;
            height: 100px;
            flex-shrink: 0;
            margin-right: 10px;

            img {
              object-position: center;
              width: 100%;
              height: 100%;
              border-radius: 5px;
              object-fit: cover;
            }
          }

          .home-right {
            font-family: aaa;

            p {
              font-size: 20px;
              color: #302e2e;
              font-weight: bold;
              overflow: hidden;
              text-overflow: ellipsis;
              display: -webkit-box;
              -webkit-box-orient: vertical;
              -webkit-line-clamp: 2;
              margin-bottom: 5px;
            }

            .home-span1 {
              font-size: 12px;
              color: #6E6F8B;
              margin-bottom: 5px;
            }

            .home-icon {
              margin-top: 5px;
              display: flex;
              align-items: center;
              width: 100%;

              .home-icon-left {
                display: flex;
                align-items: center;
                margin-right: 5px;

              }

              .home-icon-right {
                display: flex;
                align-items: center;
                // margin-right: 20px;
              }

              .icon-1 {
                width: 20px;
                height: 20px;
                display: inline-block;
                background-size: cover;
                background-position: center;
                background-repeat: no-repeat;
                border-radius: 50%;
                // margin-right: 5px;
              }

              .home-icon-yanj {
                background-image: url("../assets/image/list-yanj.png");
              }

              .home-icon-xingx {
                background-image: url("../assets/image/list-xingx.png");
              }
            }
          }
        }
      }
    }
  }
}
</style>
